<%-- Page show a list of trainees from db. --%>

<%-- Define page tag lib. --%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="com.kha.Trainee"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%-- Specify error page. --%>
<%@ page errorPage="WEB-INF/errorpage.jsp"%>

<%
	// Get trainee bean.
	Trainee trainee = (Trainee) session.getAttribute("trainee");
%>

<!DOCTYPE html>
<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Trainee List</title>
<link rel="stylesheet" type="text/css" href="vendor/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="vendor/css/docs.css">
<link rel="stylesheet" type="text/css" href="vendor/css/docs_list.css">

</head>

<body>

	<div class="navbar navbar">
		<div class="navbar-inner">

			<div class="container">
				<a class="brand" href="#">Trainee Registration</a>
			</div>

		</div>
	</div>

	<div class="container-fluid">

		<div class="row-fluid">

			<%
			
				// Check bean, redirect to homepage if not valid.
				if (trainee == null || trainee.isValid() != true) {

					response.sendRedirect("index.jsp");

				}

				String hidden = request.getParameter("save");

				if (hidden != null) { // Save action is triggered.
					%>
		
					<jsp:include page="WEB-INF/action/save.jsp" />
		
					<%
				}
				
			%>

			<!-- Show user information, data from session. -->
			<div class="bs-docs-example span3" style="height: 300px;">

				<label for="account">Account</label> <em id="account"><%=trainee.getAccount()%></em>

				<label for="firstname">Firstname</label> <em id="firstname"><%=trainee.getFirstname()%></em>

				<label for="lastname">Lastname</label> <em id="lastname"><%=trainee.getLastname()%></em>

				<label for="email">Email</label> <em id="email"><%=trainee.getEmail()%></em>
				<hr />
				<!-- Save and Update action. -->
				<div class="container-fluid">
					<div class="row-fluid">
	
						<form action="index.jsp" class="span6" method="post">
							<input type="hidden" name="update" value="update" />
							<button type="submit" class="btn">Update</button>
						</form>

						<form action="trainees.jsp" class="span6" method="post">
							<input type="hidden" name="save" value="save" />
							<button type="submit" class="btn btn-success">Save</button>
						</form>
					</div>

				</div>
			</div>

			<div class="bs-docs-example2 span8" style="overflow-y: scroll; overflow-x: hidden; height: 480px;">

				<%-- Get list of trainees from db. --%>
				<jsp:include page="WEB-INF/action/show.jsp"></jsp:include>

				<table class="table table-bordered">
					<thead>
						<tr>
							<th>Account</th>
							<th>Firstname</th>
							<th>Lastname</th>
							<th>Email</th>
						</tr>
					</thead>
					<tbody>

						<%
							// Get list pulled from db.
							ResultSet result = (ResultSet) session.getAttribute("result");

							while (result.next()) {
								String account = result.getString("account");
								String firstname = result.getString("firstname");
								String lastname = result.getString("lastname");
								String email = result.getString("email");

								out.print("<tr>");
								out.print("<td>" + account + "</td>");
								out.print("<td>" + firstname + "</td>");
								out.print("<td>" + lastname + "</td>");
								out.print("<td>" + email + "</td>");
								out.print("<tr>");

							}
							
							// Get conn and stmt from session to properly close.
							
							Connection conn = (Connection) session.getAttribute("connection");
							Statement stmt = (Statement) session.getAttribute("statement");

							result.close();
							stmt.close();
							conn.close();

							
							// Get rid of useless objects in session
							session.removeAttribute("connection");
							session.removeAttribute("result");
							session.removeAttribute("statement");
						%>

					</tbody>
				</table>
			</div>
		</div>

	</div>
</body>
</html>